/**
 * 企业试吃下单
 */
var companyEatApplysMask = new Ext.LoadMask(Ext.getBody(), {
			msg : "正在提交,请稍后..."
		});
var queryCompanyEatApplyOrderSearch = function() {
	var queryCompanyEatApplyOrderFieldSet = new Ext.form.FieldSet({
		id : 'queryCompanyEatApplyOrderFieldSet',
		title : '查询条件',
		collapsible : true,// 可折叠
		buttonAlign : 'center',
		keys : [{
					key : 13, // 13代表回车
					fn : queryCompanyEatApplyOrder,
					scope : this
				}],
		buttons : [{
					text : '查询',
					handler : function() {
						queryCompanyEatApplyOrder();
					}
				}, {
					text : '重置',
					handler : function() {
						Ext.getCmp('queryCompanyEatApplyOrderForm').getForm()
								.reset();
					}
				}],
		items : [{
			layout : 'column',
			border : false,
			items : [{
						columnWidth : .25,
						layout : 'form',
						border : false,
						items : [{
									xtype : 'textfield',
									anchor : '100%',
									fieldLabel : '公司名称',
									name : "companyName"
								}]
					}, {
						columnWidth : .25,
						layout : 'form',
						border : false,
						items : [{
									xtype : 'textfield',
									anchor : '100%',
									fieldLabel : '申请人',
									name : 'applyUserName'
								}]
					}, {
						columnWidth : .25,
						layout : 'form',
						border : false,
						items : [{
									xtype : 'textfield',
									anchor : '100%',
									fieldLabel : '申请人手机号',
									name : 'applyUserPhone'
								}]
					}, {
						columnWidth : .25,
						layout : 'form',
						border : false,
						items : [{
							xtype : 'localcommonbox',
							anchor : '100%',
							fieldLabel : '申请状态',
							hiddenName : 'applyStatus',
							loaclarr : [ ['1', '申请通过'],
									['3', '已下单'], ['', '全部']],
							value : 1
						}]
					}]
		}, {
			layout : 'column',
			border : false,
			items : [{
						columnWidth : .25,
						layout : 'form',
						border : false,
						items : [{
									xtype : 'textfield',
									anchor : '100%',
									fieldLabel : '公司地址',
									name : "companyAddress"
								}]
					}, {
						columnWidth : .25,
						layout : 'form',
						border : false,
						items : [{
									xtype : 'textfield',
									anchor : '100%',
									fieldLabel : '申请部门',
									name : "applyDepartment"
								}]
					}, {
						columnWidth : .25,
						layout : 'form',
						border : false,
						items : [{
									xtype : 'textfield',
									anchor : '100%',
									fieldLabel : '公司电话',
									name : "companyTel"
								}]
					}]
		}]
	});
	fieldSetListeners(['queryCompanyEatApplyOrderFieldSet',
					'queryCompanyEatApplyOrderForm', 'companyEatApplyOrderManagerGrid'],
			[0.23, 0.77]);

	var queryCompanyEatApplyOrderForm = new Ext.form.FormPanel({
				id : 'queryCompanyEatApplyOrderForm',
				anchor : '100% 23%',
				frame : true,
				labelAlign : 'right',
				bodyBorder : false,
				items : [queryCompanyEatApplyOrderFieldSet]
			});
	return queryCompanyEatApplyOrderForm;
};

/**
 * grids
 */
var companyEatApplyOrderManagerGrid = function() {
	var companyEatApplyManagerSm = new Ext.grid.CheckboxSelectionModel();// 创建选择模式对象
	// 定义表的每列（对应表头信息）
	var companyEatApplyManagerCm = new Ext.grid.ColumnModel([
			companyEatApplyManagerSm, {
				dataIndex : "id",
				header : "企业试吃编号",
				width : 90
			}, {
				dataIndex : "companyName",
				header : "公司名称",
				sortable : true,
				width : 80
			}, {
				dataIndex : "companyAddress",
				header : "公司地址",
				sortable : true,
				width : 130,
				renderer : function(value, meta, record) {
					if (value) {
						meta.attr = 'ext:qtitle="公司地址:" ext:qtip="' + value
								+ '"';
						return value;
					}
				}
			}, {
				dataIndex : "employeeNum",
				header : "试吃人数",
				sortable : true,
				width : 60
			}, {
				dataIndex : "applyDepartment",
				header : "申请部门",
				sortable : true,
				width : 80
			}, {
				dataIndex : "applyUserName",
				header : "申请人",
				sortable : true,
				width : 70
			}, {
				dataIndex : "applyUserPhone",
				header : "申请人手机号",
				sortable : true,
				width : 90
			}, {
				dataIndex : "companyTel",
				header : "公司电话",
				sortable : true,
				width : 80
			}, {
				dataIndex : "companyEmail",
				header : "公司邮箱",
				sortable : true,
				width : 100
			}, {
				dataIndex : "companyScale",
				header : "公司规模",
				sortable : true,
				width : 80
			}, {
				dataIndex : "applyStatus",
				header : "申请状态",
				sortable : true,
				width : 80,
				renderer : function(value) {
					return PmsUtil.ApplyStatus(value);
				}
			}, {
				dataIndex : "orderId",
				header : "订单号",
				sortable : true,
				width : 60,
				renderer : function(orderId) {
					var returnValue = "";
					if (orderId) {
						returnValue = '<a href ="#" onClick="lookOrderManager(\''
								+ orderId + '\')">' + orderId + '</a>';
					}
					return returnValue;
				}
			}, {
				dataIndex : "noPassReason",
				header : "申请不通过原因",
				sortable : true,
				width : 100
			}, {
				dataIndex : "isConnection",
				header : "联系状态",
				sortable : true,
				width : 60,
				renderer : function(value) {
					return PmsUtil.applyEatIsConnection(value);
				}
			}, {
				dataIndex : "connectionDate",
				header : "联系时间",
				sortable : true,
				width : 130
			}, {
				dataIndex : "createdDate",
				header : "创建时间",
				sortable : true,
				width : 130
			}, {
				dataIndex : "createdName",
				header : "创建人",
				sortable : true,
				width : 60
			}, {
				dataIndex : "updatedDate",
				header : "修改时间",
				sortable : true,
				width : 130
			}, {
				dataIndex : "updatedName",
				header : "修改人",
				sortable : true,
				width : 60
			}, {
				header : "管理",
				width : 230,
				dataIndex : "id",
				sortable : false,
				renderer : function(j, i, g, m, h) {
					var id = g.data.id;
					var applyStatus = g.data.applyStatus;
					var isConnection = g.data.isConnection;
					var k = "";
					if (isGranted("companyEatApply_编辑")) {
						if (applyStatus == 0) {
							k += '&nbsp;&nbsp;<a href="#" onclick="updCompanyEatApply('
									+ id + ')\">修改</a>';
						}

					}

					if (isGranted("companyEatApplyOrder_下单")) {
						if (applyStatus == 1) {
							k += '&nbsp;&nbsp;<a href="#" onclick="companyEatApplyOrderPass('
									+ id + ')\" >下单</a>';
						}
					}

					if (isGranted("companyEatApply_不通过")) {
						if (applyStatus == 0) {
							k += '&nbsp;&nbsp;<a href="#" onclick="companyEatApplyNoPass('
									+ id + ')\">申请不通过</a>';
						}
					}
                    
				
					return k;
				}
			}]);

	// 定义分析器
	var companyEatApplyOrderManagerReader = new Ext.data.JsonReader({
				totalProperty : "totalCount",
				root : "list",
				fields : [{
							name : "id"
						}, {
							name : "companyName"
						}, {
							name : "companyAddress"
						}, {
							name : "employeeNum"
						}, {
							name : "applyDepartment"
						}, {
							name : "applyUserName"
						}, {
							name : "applyUserPhone"
						}, {
							name : "companyTel"
						}, {
							name : "companyEmail"
						},{
							name : "companyScale"
						}, {
							name : "goodsPriceSizeId"
						}, {
							name : "applyStatus"
						}, {
							name : "noPassReason"
						}, {
							name : "isConnection"
						}, {
							name : "connectionDate"
						}, {
							name : "orderId"
						}, {
							name : "createdName"
						}, {
							name : "updatedDate"
						}, {
							name : "updatedName"
						}, {
							name : "createdDate"
						}]
			});

	// 取数据store
	var companyEatApplyOrderStore = new Ext.data.Store({
				autoLoad : true,// 要加载数据必须设置为true
				proxy : new Ext.data.HttpProxy({
							url : __ctxPath
									+ "/companyEatApplyManager/selectCompanyEatApply"
						}),
				remoteSort : false,
				baseParams : {
					start : 0,
					limit : 25,
					applyStatusArray:'1,3'
					
				},
				listeners : {
					beforeload : {
						fn : function(thiz, options) {
							Ext.apply(thiz.baseParams, Ext
											.getCmp('queryCompanyEatApplyOrderForm')
											.getForm().getValues());
						}
					}
				},
				reader : companyEatApplyOrderManagerReader
			});

	// 定义grid的toolbar
	var companyEatApplyOrderTb = new Ext.Toolbar({
				id : "companyEatApplyOrderTb",
				height : 25,
				items : []
			});
	companyEatApplyOrderTb.add(new Ext.scm.Button({
				text : "刷 新",
				iconCls : "btn-refresh",
				width : 80,
				hsValue : 'companyEatApplyOrderManager_刷新',
				pressed : true,
				handler : function() {
					queryCompanyEatApplyOrder();
				}
			}));

	companyEatApplyOrderTb.add(new Ext.Toolbar.Spacer());
	companyEatApplyOrderTb.add(new Ext.scm.Button({
				text : "导出excel",
				iconCls : "btn-excel",
				width : 80,
				pressed : true,
				hsValue : "companyEatApplyOrderManager_批量导出",
				listeners : {
					"click" : function() {
						exportCompanyEatApplyToExcel();
					}
				}
			}));

	var companyEatApplyOrderManagerGrid = new Ext.grid.GridPanel({
				id : 'companyEatApplyOrderManagerGrid',
				store : companyEatApplyOrderStore,
				cm : companyEatApplyManagerCm,
				sm : companyEatApplyManagerSm,
				autoScroll : true,
				loadMask : true,
				stripeRows : true,
				columnLines : true,
				loadMask : {
					msg : "正在加载数据，请稍侯……"
				},
				viewConfig : {
					emptyText : '没有您想要的数据!'
				},
				anchor : '100% 77%',
				frame : true,
				tbar : companyEatApplyOrderTb,
				bbar : new PG.PagingBar({
							store : companyEatApplyOrderStore
						})
			});
	return companyEatApplyOrderManagerGrid;
};

// 查询
function queryCompanyEatApplyOrder() {
	var grid = Ext.getCmp("companyEatApplyOrderManagerGrid");
	var form = Ext.getCmp("queryCompanyEatApplyOrderForm");
	if (form.getForm().isValid()) {
		grid.getStore().load();
		
	}
}


/**
 * 企业试吃下单操作
 * 
 * @param {}
 *         id
 */
function companyEatApplyOrderPass(id) {
	Ext.Loader.load([__ctxPath + '/js/avtivity/companyApplyEatGoodsTree.js'],
			function() {
				saveCompanyApplyEatTreeWin(id, "选择商品").show();
			});
}

// 修改试吃申请对象
function updCompanyEatApply(id) {
	try {
		updCompanyEatApplyWin(id, "编辑试吃申请").show();
	} catch (Error) {
		Ext.Loader.load([__ctxPath + '/js/avtivity/companyEatApplyForm.js'],
				function() {
					updCompanyEatApplyWin(id, "编辑试吃申请").show();
				});
	}
}

// 跳转到订单查询页面
function lookOrderManager(orderId) {
	var centerPanel = App.getContentPanel();
	var tab = centerPanel.getItem(PmsUtil.ORDER_MESSAGE);
	var createNewTab = new CreatNewTab(PmsUtil.ORDER_MESSAGE, '订单查询');
	if (tab == null) {
		createNewTab.createNew(tab);
	} else {
		createNewTab.createOld(tab, ['queryOrdersFieldSet', 'queryOrderForm',
						'orderManagerGrid']);
	}
	Ext.Loader.load([__ctxPath + '/js/order/orderManager.js'], function() {
				Ext.getCmp('orderManager_id').setValue(orderId);
				Ext.getCmp('orderManager_startDate').setValue("");
				Ext.getCmp('orderManager_endDate').setValue("");
			});
}

// 企业试吃的excel导出
function exportCompanyEatApplyToExcel() {
	var d = Ext.getCmp("companyEatApplyOrderManagerGrid");
	var jsonrecord = d.getSelectionModel().getSelections();
	if (jsonrecord.length == 0) {
		Ext.MessageBox.show({
					title : "操作信息",
					msg : "请选择要导出的订单!",
					buttons : Ext.MessageBox.OK,
					icon : Ext.MessageBox.WARNING
				});
		return;
	}

	var ids = "";
	var m = 0;
	for (var i = 0; i < jsonrecord.length; i++) {
		var tmp = jsonrecord[i];
		if (m > 0) {
			ids = ids + "," + tmp.data.id;
		} else {
			ids = tmp.data.id;
		}
		m++;
	}
	Ext.Msg.confirm("信息确认", "是否批量导出企业试吃申请数据？", function(btn) {
		if (btn == 'yes') {
			document.location.href = __ctxPath
					+ "/companyEatApplyManager/exportCompanyEatApplyToExcel?ids="
					+ ids;
		}

	});
};



Ext.getCmp("centerTabPanel").getActiveTab().add(queryCompanyEatApplyOrderSearch(),
		companyEatApplyOrderManagerGrid());
Ext.getCmp("centerTabPanel").getActiveTab().doLayout();